<template>
  <div style="width: 100%; height: 100%">
      <!-- 左部 -->
      <div class="leftback">
        <div class="orderJuzao">
          <div class="orderJuzaoText">ORDER聚造</div>
          <div style="margin-left:20px; line-height: 39px">
            <i class="el-icon-s-unfold" style="color: #ffffff"></i>
          </div>
        </div>

        <el-menu
          default-active="2"
          class="el-menu-vertical-demo"
          @open="handleOpen"
          @close="handleClose"
          background-color="transparent"
          text-color="#fff"
          :router="true"
          active-text-color="#ffd04b"
          style="margin-top: 25px; width: 100%"
        >
          <el-submenu index="1">
            <template slot="title">
              <i class="el-icon-s-home"></i>
              <span>首页管理</span>
            </template>
            <el-menu-item-group>
              <el-menu-item index="/index/banners" 
                >轮播图管理</el-menu-item
              >
              <el-menu-item index="/index/category" 
                >分类区管理</el-menu-item
              >
              <el-menu-item index="/index/recomend" 
                >为你推荐管理</el-menu-item
              >
            </el-menu-item-group>
          </el-submenu>

          <el-submenu index="2">
            <template slot="title">
              <i class="el-icon-s-goods"></i>
              <span>商品管理</span>
            </template>
            <el-menu-item-group>
              <el-menu-item index="/index/pinpai" >品牌管理</el-menu-item>
              <el-menu-item index="/index/danpin" >单品品管理</el-menu-item>
              <el-menu-item index="/index/sp" >商品管理</el-menu-item>
              <el-menu-item index="/index/fenlei" >分类管理</el-menu-item>
            </el-menu-item-group>
          </el-submenu>

          <el-menu-item index="/index/news">
            <i class="el-icon-s-management"></i>
            <span slot="title">新闻管理</span>
          </el-menu-item>
        </el-menu>
      </div>

      <!-- 右部 -->
      <div class="rightback">
        <div style="margin-left: 250px">
          <div class="right1">
            <b style="line-height: 51px;width:550px;">
            {{ $route.meta.title }}
            </b>
            <div style="margin-left: 294px">
              <el-avatar :src="photo" :size="50"></el-avatar>
            </div>
            
            <el-tooltip placement="bottom">
              <div slot="content" @click="dialogFormVisible = true" class="cursor">修改密码</div>
              <span class="right12">{{ nickName }}</span>
            </el-tooltip>
            <div class="right13">小王，您好！</div>

            <el-button
              type="danger"
              round
              @click="logout"
              size="small"
              icon="el-icon-switch-button"
              class="ad"
              >
              退出
              </el-button>
          </div>

          <div style="margin-top:20px">
            <router-view></router-view>
          </div>
        </div>
      </div>

    <el-dialog title="修改密码" :visible.sync="dialogFormVisible" width="400px">
      <el-form :model="form" ref="form" :rules="rules" label-width="100px">
        <el-form-item label="原密码" prop="passWord">
          <el-input type="password" v-model="form.passWord"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newPassWord">
          <el-input type="password" v-model="form.newPassWord"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="reNewPassWord">
          <el-input type="password" v-model="form.reNewPassWord"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false" style="width: 60px; height: 40px">取 消</el-button >
        <el-button @click="changePassWord" type="primary" style="width: 60px; height: 40px">确 定</el-button>
      </div>
    </el-dialog>
  </div>

  <!-- <router-link to="/login">go to login</router-link> -->
</template>

<script>
import md5 from "js-md5";

export default {
  name: "index",
  components: {},
  data() {
    return {
      dialogFormVisible: false,
      photo: "",
      nickName: "",
      value1: "",
      form: {
        passWord: "",
        newPassWord: "",
        reNewPassWord: "",
      },
      group: {
        //选中的数组
        groupId: "",
        groupName: "",
      },
      menus: [], //菜单树
      rules: {
        passWord: [
          { require: true, $message: "请输入原密码", trigger: "blur" },
        ],
        newPassWord: [
          { require: true, $message: "请输入新密码", trigger: "blur" },
        ],
        reNewPassWord: [
          { require: true, $message: "请重复输入新密码", trigger: "blur" },
          {
            validator: (rule, value, callback) => {
              if (value === "") {
                callback(new Error("请再次输入密码"));
              } else if (value !== this.form.newPassWord) {
                callback(new Error("两次输入密码不一致!"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
      },
    };
  },

  methods: {
    handleOpen(key, keyPath) {
      console.log(key, keyPath);
    },
    handleClose(key, keyPath) {
      console.log(key, keyPath);
    },
   logout() {
      this.$confirm("是否退出登录?","提示", {
        confirmButtonText:"确定",
        concelButtonText:"取消",
        type:"warning",
      }).then(() => {
        this.$http({
          url:"/admin/logout",
        }).then((res) => {
          if(res.errorMsg) {
            this.$message.error(res.errorMsg);
          }else{
            console.log('sasdawdwad')
            localStorage.removeItem('token')
            localStorage.removeItem('user')
            location.href = "/login";   
          }
        })
      })
    },
    changePassWord() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.$http({
            url: "/admin/changePassWord",
            method: "POST",
            data: {
              passWord: md5(this.form.passWord),
              newPassWord: md5(this.form.newPassWord),
              // userName: this.form.userName,
            },
          }).then((res) => {
            if (!res.errorMsg) {
              //this.dialogFormVisible = false
              this.$message({
                message: "修改密码成功",
                type: "success",
                duration: 3000,
              });

              setTimeout(() => {
                location.href = "#/login";
              }, 1000);
            }
          });
        } else {
          return false;
        }
      });
    },
  },
  created() {
    console.log(this.$route);
    console.log("~~~~~~~~~");
    console.log(this.$router);

    //从缓存获取登录用户信息
    let user = localStorage.getItem("user");
    if (!user) {
      //this.$router.push('/login')
      console.log(history, location);
      location.href = location.origin + "#/login";
      //history.reload
    } else {
      //解析为对象
      user = JSON.parse(user);
      //设置头像
      this.photo = user.avatarUrl || require("../assets/touxiang.png");
      //设置昵称
      this.nickName = user.nickName || "小王";
      //获取菜单
      //this.getMenus()
    }
  },
  mounted() {},
};
</script>

<style scoped>
* {
  margin: 0; /* 去除外边距和内边距 */
  padding: 0;
}

/* 左部 */
.leftback{
    width: 210px;
    height: 810px;
    background-image: url(../assets/rightback.png);
    background-repeat: no-repeat;
    position:fixed;
    background-size: cover;
}
.orderJuzao {
  width: 100%;
  height: 30px;
  display: flex;
}

.orderJuzaoText{
    width: 105px;
    height: 24px;
    font-size: 19px;
    font-family: MicrosoftYaHeiUI-Bold-, MicrosoftYaHeiUI-Bold;
    color: #FFFFFF;
    padding-top: 6px;
    margin-left: 35px;
}



/* 右部 */
.rightback {
  background-color: #eff6fc;
  /* margin-left: 60px;
  width:1440px; */
  overflow-x: hidden;
  width: 100%;
  height: 100%;

}

.right1 {
  margin-top: 28px;
  display: flex;
  width: 100%;
  height: 51px;
  position: flexd;
}

.ad {
  width: 75px;
  height: 35px;
  margin-top: 8px;
  justify-content: center; /* 垂直横向居中，必须结合flex */
  align-items: center;
}

.right11 {
  margin-left: 35px;
  margin-top: 13px;
  width: 154px;
  height: 21px;
  font-size: 17px;
  font-family: MicrosoftYaHeiUI-Bold-, MicrosoftYaHeiUI-Bold;
  font-weight: normal;
  color: #2c2c2c;
}

.tu5 {
  width: 51px;
  height: 51px;
  margin-left: 708px;
}

.right12 {
  margin-left: 8px;
  color: #eab924;
  margin-top: 15px;
  height: 20px;
  font-size: 15px;
  font-family: MicrosoftYaHei-, MicrosoftYaHei;
  font-weight: normal;
}

.right13 {
  margin-left: 5px;
  margin-top: 15px;
  height: 20px;
  font-size: 15px;
  font-family: MicrosoftYaHei-, MicrosoftYaHei;
  font-weight: normal;
  color: #2c2c2c;
}

.right14 {
  width: 75px;
  height: 31px;
  background: #f56c6c;
  border-radius: 15px 15px 15px 15px;
  opacity: 1;
  margin-left: 4px;
  margin-top: 11px;
  display: flex;
}

.tu6 {
  width: 15px;
  height: 15px;
  margin-top: 7px;
  margin-left: 8px;
}

.right141 {
  margin-left: 8px;
  margin-top: 6px;
  width: 75px;
  height: 47px;
  font-size: 13px;
  font-family: MicrosoftYaHei-, MicrosoftYaHei;
  font-weight: normal;
  color: #ffffff;
}


</style>